package cn.tedu.rms.branch.service.impl;


import cn.tedu.rms.branch.mapper.BranchMapper;
import cn.tedu.rms.branch.pojo.dto.BranchQuery;
import cn.tedu.rms.branch.pojo.dto.BranchSaveParam;
import cn.tedu.rms.branch.pojo.vo.BranchVO;
import cn.tedu.rms.branch.service.BranchService;
import cn.tedu.rms.comment.pojo.dto.CommentQuery;
import cn.tedu.rms.comment.pojo.vo.CommentVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.tedu.rms.branch.pojo.entity.Branch;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class BranchServiceImpl implements BranchService {
    @Autowired
    BranchMapper branchMapper;

    @Override
    public List<BranchVO> selectBranch(BranchQuery branchQuery) {

        log.debug("查询分店业务,branchQuery:{}",branchQuery);
        List<BranchVO> list=branchMapper.selectBranch(branchQuery);
        for(BranchVO branchVO : list){
            toLonLat(branchVO);
        }
        log.debug("查询分店的信息集合,branchVOList:{}",list);
        return list;
    }

    @Override
    public BranchVO selectById(Long id) {
        log.debug("根据id查询分店业务，参数：{}",id);
        BranchVO branchVO = branchMapper.selectById(id);
        toLonLat(branchVO);
        log.debug("查询分店的信息,branchVO:{}",branchVO);
        return branchVO;
    }



    @Override
    public void updateStatusById(Long id, String status) {
        log.debug("根据id修改分店状态，参数：{}，{}",id,status);
        Branch branch = new Branch();
        branch.setId(id);
        branch.setStatus(status);
        branchMapper.update(branch);
    }

    @Override
    public void saveBranch(BranchSaveParam branchSaveParam) {
        log.debug("分店新增、保存业务参数:{}",branchSaveParam);
        Branch branch = new Branch();
        BeanUtils.copyProperties(branchSaveParam,branch);
        if(branchSaveParam.getId()==null){
            //新增业务
            branch.setCreateTime(new Date());
            branch.setStatus("0");
            branchMapper.insert(branch);
        }else{
            //修改业务
            branch.setUpdateTime(new Date());
            branchMapper.update(branch);
        }
    }

    @Override
    public void deleteBranchById(Long id) {
        log.debug("根据分店id删除业务，参数：{}",id);
        branchMapper.deleteById(id);
    }

    @Override
    public List<BranchVO> selectByContent(String content) {
        log.debug("根据搜索框内容查询分店业务，参数：{}",content);
        List<BranchVO> branchVOList = branchMapper.selectByContent(content);
        for(BranchVO branchVO : branchVOList){
            toLonLat(branchVO);
        }
        log.debug("查询分店的信息集合,branchVOList:{}",branchVOList);
        return branchVOList;
    }

    @Override
    public BranchVO selectBranchComment(CommentQuery commentQuery) {
        log.debug("根据条件查询分店评论业务，参数：{}",commentQuery);

        BranchVO branchVO =branchMapper.selectBranchComment(commentQuery);
        if(branchVO==null){
            //此时该分店没有评论或者评论状态全部为禁用状态
            commentQuery.setStatus("");
            branchVO =branchMapper.selectBranchComment(commentQuery);
            List<CommentVO> commentVOList = branchVO.getCommentVOList();
            if(commentVOList!=null&&!commentVOList.isEmpty()){
                log.debug("评论信息不为空，开始过滤");
                branchVO.setCommentVOList(null);
            }
        }
        log.debug("查询分店的评论信息,branchVO:{}",branchVO);

        return branchVO;
    }

    /*转换经纬度*/
    public void toLonLat(BranchVO branchVO){
        //{"type":"point","recPoints":"111.1111,187.898787"}
            String position = branchVO.getPosition();
            int start = position.lastIndexOf(':')+2;
            int end = position.lastIndexOf('"');
            String str = position.substring(start,end);//111.1111,45.898787 经度,纬度
            String[] lngLat = str.split(",");
            String lng = lngLat[0];//经度
            String lat = lngLat[1];//纬度
            branchVO.setLng(lng);
            branchVO.setLat(lat);
    }


}
